import com.epwk.action.Action;
import com.epwk.action.MouseAction;
import com.epwk.driver.SeleniumDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import Action.*;

/**
 * Created by Administrator on 2016/11/21.
 */
public class Release {
    WebDriver driver;


    @BeforeTest
    public void openBrowser(){
        driver = SeleniumDriver.openBrowser("chrome");
        SeleniumDriver.maxBrowser();
    }

    @Test
    public void releaseTask() throws InterruptedException {

//        登陆

        Login.login(driver,"wuyy001","123654");
//        进入发布任务页面
        Action.click(By.xpath(".//*[text()=\"发布任务\"]"));
//        driver.findElement(By.xpath(".//*[text()=\"发布任务\"]")).click();

//        选择分类
        Action.click(By.id("eiditCaty"));
//        driver.findElement(By.id("eiditCaty")).click();
        Thread.sleep(3000);
        MouseAction.moveToElement(By.xpath(".//*[text()=\"找开发\"]"));
//        WebElement dalei = driver.findElement(By.xpath(".//*[text()=\"找开发\"]"));
//        Actions action = new Actions(driver);
//        action.moveToElement(dalei).perform();
        Action.click(By.xpath(".//*[text()=\"游戏代练\"]"));
//        driver.findElement(By.xpath(".//*[text()=\"游戏代练\"]")).click();

//        任务标题和详情
        Action.sendkeys(By.id("txt_title"),"这个是个任务标题");
//        driver.findElement(By.id("txt_title")).sendKeys("这个是个任务标题");
        driver.switchTo().frame("xhe0_iframe");
        Action.sendkeys(By.className("editMode"),"这个是任务详情");
//        driver.findElement(By.className("editMode")).sendKeys("这个是任务详情");
        driver.switchTo().defaultContent();

//        联系方式
        Action.click(By.xpath(".//*[@id='cont_type_chzn']/a/span"));
//        driver.findElement(By.xpath(".//*[@id='cont_type_chzn']/a/span")).click();
//        Thread.sleep(3000);
        Action.click(By.id("cont_type_chzn_o_1"));
        Action.clear(By.id("tar_contact"));
        Action.sendkeys(By.id("tar_contact"),"2277144876");
//        driver.findElement(By.id("cont_type_chzn_o_1")).click();
//        driver.findElement(By.id("tar_contact")).clear();
//        driver.findElement(By.id("tar_contact")).sendKeys("2277144876");

//        滚动滚动条
        JavascriptExecutor slide = (JavascriptExecutor)driver;
        slide.executeScript("var q=document.documentElement.scrollTop=800");
        Thread.sleep(3000);

//        选择交易模块
        Action.click(By.xpath(".//*[@id='frm_step']/div[1]/div[5]/div[4]/span/span"));
//        driver.findElement(By.xpath(".//*[@id='frm_step']/div[1]/div[5]/div[4]/span/span")).click();
        if(driver.findElement(By.xpath(".//*[@id='frm_step']/div[1]/div[5]/div[2]/span/span")).isSelected()){
//            招标模式
//            选择招标任务还是雇佣任务
            driver.findElement(By.xpath(".//*[text()='我有具体的预算金额，直接填写赏金数目']")).click();
            if(driver.findElement(By.xpath(".//*[text()='我有具体的预算金额，直接填写赏金数目']")).isSelected()){
//                雇佣任务
//                雇佣金额
                driver.findElement(By.id("txt_task_cash2")).sendKeys("5000");
            }else if (driver.findElement(By.xpath(".//*[text()='我没有具体的预算金额，选择价格区间']")).isSelected()){
//                招标任务
//                招标金额
                WebElement cashSelect = driver.findElement(By.id("slt_task_cash_coverage"));
                Select select = new Select(cashSelect);
//                选择招标金额：0代表100-1000元；1代表1000-5000元；2代表5000-10000；3代表10000-3000；4代表3w-5w；5代表5w-10w；6代表10w以上
                select.deselectByIndex(0);
            }
//            截止时间
            driver.findElement(By.id("calendarLastday")).clear();
            driver.findElement(By.id("calendarLastday")).sendKeys("2016-11-03");

//            滚动滚动条
            JavascriptExecutor slide01 = (JavascriptExecutor)driver;
            slide.executeScript("var q=document.documentElement.scrollTop=1300");
            Thread.sleep(3000);

//            对威客的要求
//            诚信
            driver.findElement(By.xpath(".//*[text()=\"保证完成\"]")).click();
            driver.findElement(By.xpath(".//*[text()=\"保证售后\"]")).click();
            driver.findElement(By.xpath(".//*[text()=\"保证原创\"]")).click();
//            地域
            driver.findElement(By.linkText("限制地域")).click();
            if(driver.findElement(By.linkText("限制地域")).isSelected()){
                Actions diyu1 = new Actions(driver);
                WebElement province = driver.findElement(By.xpath(".//*[@id='prov_city_limit']/div[1]/em"));
                diyu1.moveToElement(province).perform();
                driver.findElement(By.xpath(".//*[text()=\"福建省\"]")).click();
                Actions diyu2 = new Actions(driver);
                WebElement city = driver.findElement(By.xpath(".//*[@id='prov_city_limit']/div[2]/em"));
                diyu2.moveToElement(city).perform();
                driver.findElement(By.xpath(".//*[text()=\"厦门市\"]")).click();
            }
        }else {
//            悬赏模式
            driver.findElement(By.id("txt_task_cash1")).sendKeys("1000");
//            选择单赏、多赏或是计件
            driver.findElement(By.xpath(".//*[text()='单人悬赏，只设置一个中标者']")).click();
            if (driver.findElement(By.xpath(".//*[text()='多人悬赏，设置多人中标分享赏金']")).isSelected()){
//                多赏任务
//                确定几个人参加多赏任务
                WebElement pNum = driver.findElement(By.id("slt_task_cash_coverage"));
                Select select = new Select(pNum);
//                参与人数：0代表2人；1代表3人；2代表4人；3代表5人
                select.selectByIndex(0);
//                设置一等奖、二等奖赏金
                driver.findElement(By.id("task_prize_1")).sendKeys("600");
                driver.findElement(By.id("task_prize_2")).sendKeys("400");
            }else if (driver.findElement(By.xpath(".//*[text()='计件悬赏，合格一稿，支付一稿（稿件数量≥2件）']")).isSelected()){
//                计件任务
//                设置计件稿件数
                driver.findElement(By.id("txt_work_count")).sendKeys("4");
            }
            Thread.sleep(3000);

//            设置截止时间
            driver.findElement(By.id("calendarLastday_xs")).clear();
            driver.findElement(By.id("calendarLastday_xs")).sendKeys("2016-11-03");

//            滚动滚动条
            JavascriptExecutor slide01 = (JavascriptExecutor)driver;
            slide.executeScript("var q=document.documentElement.scrollTop=1800");
            Thread.sleep(3000);

//            设置威客要求
            Thread.sleep(3000);
            JavascriptExecutor js = (JavascriptExecutor)driver;
            String cash = (String) js.executeScript("(function(){ var value=document.getElementById('txt_task_cash1').value;document.getElementById('txt_task_cash1').setAttribute('temp',value)})()");
            int cashint = Integer.parseInt(driver.findElement(By.id("txt_task_cash1")).getAttribute("temp"));
            if (cashint > 1000){
                JavascriptExecutor js1 = (JavascriptExecutor)driver;
                JavascriptExecutor js2 = (JavascriptExecutor)driver;
                js1.executeScript("$(\".resdenjibox_btn\").css({'left':'245px'})");
                js2.executeScript("$(\".resdenjibox_x\").width(\"49%\")");
            }
        }

    }

//    @AfterTest
//    public void closeBrowser() throws InterruptedException {
//        Thread.sleep(3000);
//        driver.quit();
//    }
}
